package edu.neepu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class GetText implements Spider{
    String urlSegment = "";
    String bookName = "";
    String chapName = "";
    int count;
    GetText(String urlSegment, String bookName, String chapName, int count){
        this.urlSegment = urlSegment;
        this.bookName = bookName;
        this.chapName = chapName;
        this.count = count;
    }
    public void getText() {
        getBookText();
    }
    /**
     *  urlSegment：每一章的地址片段
     *  bookName：中文书名
     *  chapName：每一章的名称
     *  count：当前是第几章
     */
    public void getBookText() {
        int retry = 0;
        String url = "http://www.shicimingju.com" + urlSegment;
        while (true) {
            try {
                Document document = Jsoup.connect(url).timeout(10000).get();
                Elements content = document.select(".www-main-container p");
                StringBuilder data = new StringBuilder();
                for (Element p : content) {
                    data.append(p.text()).append("\n");
                }
                //String data = content.text();
                //存入文本文件中
                saveText(data.toString());
                break;
            } catch (IOException e) {
                if (retry < 5) {
                    System.err.println(url + " 发生下载错误，重试中，原因：");
                    System.err.println(e.toString());
                    retry++;
                } else {
                    System.err.println(url + " 发生下载错误，已重试5次，放弃，原因：");
                    System.err.println(e.toString());
                    break;
                }
            }
        }
    }
    /**
     *  data：文本内容
     *  bookName：中文书名
     *  chapName：每一章的名称
     *  count：当前是第几章
     */
    public void saveText(String data){
        //处理转义字符问题
        chapName = chapName.replace("/","-");
        chapName = chapName.replace("?","-");
        chapName = chapName.replace("\"","-");
        String path = "D:/novel/"+bookName+"/";//文件夹路径
        File dirName = new File(path);
        if(!dirName.exists()){
            dirName.mkdirs();
            //System.out.println(dirName+"目录创建成功");
        }
        try {
            FileWriter writer = new FileWriter(path + chapName + count + ".txt");
            writer.write(data);
            writer.close();
            System.out.println(path + chapName + count + ".txt 下载完成");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
